package com.example.springbott.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * 上传至阿里云OSS对象储存
 * @author silic xia
 * @since 2022/11/22 22:07
 */

public class OSSUploadUntil {


    @SneakyThrows
    public static String upload(MultipartFile file) {
        String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
        String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";
        String bucket = "cskaoyan";
        String endPoint = "oss-cn-beijing.aliyuncs.com";

        String[] split = file.getOriginalFilename().split("\\.");
        String fileName = UUID.randomUUID().toString() .replaceAll("-","") + "." + split[1];

        InputStream inputStream = file.getInputStream();
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        PutObjectResult putObjectResult = ossClient.putObject(bucket, fileName, inputStream);

        String picUrl = "https://" + bucket + "." + endPoint + "/" + fileName;
        return picUrl;
    }
}

